package com.yang.rocket;

import com.yang.domain.OperationLog;
import com.yang.domain.User;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


// 发送消息
@Service
public class RocketMQProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 定义一个常量TOPIC，用于指定用户相关的主题
     * 使用final修饰，确保该常量的值在实例化后不可变
     */
    private final String TOPIC = "user-topic";


    public void sendMessage(User user, String action) {
        //获取系统当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(formatter);

        OperationLog log = new OperationLog();
        log.setUserId(user.getUserId());
        log.setAction(action);
        log.setIp("127.0.0.1");
        if ("REGISTER".equals(action)) {
            log.setDetail("用户" + user.getUsername() + "在" + currentTime + "注册成功");
        }
        if ("LOGIN".equals(action)) {
            log.setDetail("用户" + user.getUsername() + "在" + currentTime + "登录成功");
        }
        if ("PAGING_QUERY".equals(action)) {
            log.setDetail("用户" + user.getUsername() + "在" + currentTime + "分页查询用户列表");
        }
        // 发送消息
//        rocketMQTemplate.sendOneWay(TOPIC, MessageBuilder.withPayload(log).build());
        //添加重试机制
        int retryCount = 3;
        while (retryCount > 0) {
            try {
                rocketMQTemplate.sendOneWay(TOPIC, MessageBuilder.withPayload(log).build());
                break;
            } catch (Exception e) {
                retryCount--;
                if (retryCount == 0) {
                    // 记录日志或进行其他处理
                    System.err.println("消息发送失败，重试次数已用完：" + e.getMessage());
                }
            }
        }
    }

    public void sendMessage(User currentUser, User targetUser, String action) {
        //获取系统当前时间
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String currentTime = now.format(formatter);

        OperationLog log = new OperationLog();
        log.setUserId(currentUser.getUserId());
        log.setAction(action);
        log.setIp("127.0.0.1");
        if ("QUERY".equals(action)) {
            log.setDetail("用户" + currentUser.getUsername() + "在" + currentTime + "查询用户" + targetUser.getUsername() + "信息");
        }
        if ("UPDATE".equals(action)) {
            log.setDetail("用户" + currentUser.getUsername() + "在" + currentTime + "更新用户" + targetUser.getUsername() + "信息");
        }
        if ("RESET_PASSWORD".equals(action)) {
            log.setDetail("用户" + currentUser.getUsername() + "在" + currentTime + "重置用户" + targetUser.getUsername() + "密码");
        }
        if ("UPGRADE_TO_ADMIN".equals(action)) {
            log.setDetail("用户" + currentUser.getUsername() + "在" + currentTime + "将用户" + targetUser.getUsername() + "升级为管理员");
        }
        if ("DOWNGRADE_TO_USER".equals(action)) {
            log.setDetail("用户" + currentUser.getUsername() + "在" + currentTime + "将用户" + targetUser.getUsername() + "降级为普通用户");
        }
        // 发送消息
//        rocketMQTemplate.sendOneWay(TOPIC, MessageBuilder.withPayload(log).build());
        //添加重试机制
        int retryCount = 3;
        while (retryCount > 0) {
            try {
                rocketMQTemplate.sendOneWay(TOPIC, MessageBuilder.withPayload(log).build());
                break;
            } catch (Exception e) {
                retryCount--;
                if (retryCount == 0) {
                    // 记录日志或进行其他处理
                    System.err.println("消息发送失败，重试次数已用完：" + e.getMessage());
                }
            }
        }
    }
}
